//
//  NSString+Extension.m
//  正则表达式
//
//  Created by apple on 14/11/15.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)

- (BOOL)match:(NSString *)pattern
{
    // 1.创建正则表达式
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    // 2.测试字符串
    NSArray *results = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    
    return results.count > 0;
}
- (BOOL)compare:(NSString *)myString
{
    NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
    //符合數字條件的有幾個字元
    NSInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:myString
                                
                                                                      options:NSMatchingReportProgress
                                                                        range:NSMakeRange(0, myString.length)];
    //英文字條件
    
    NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];
    //符合英文字條件的有幾個字元
    
    NSInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:myString options:NSMatchingReportProgress range:NSMakeRange(0, myString.length)];
    if (tNumMatchCount == myString.length)
        
    {
        //全部符合數字，表示沒有英文
        // NSLog(@"123数字");
        return NO;
    }
    
    else if (tLetterMatchCount == myString.length)
        
    {
        //NSLog(@"345字母");
        //全不符合英文，表示沒有數字
        return NO;
    }
    
    else if (tNumMatchCount + tLetterMatchCount == myString.length)
        
    {
        //NSLog(@"456数字字母");
        //符合英文和符合數字條件的相加等於密碼長度
        return YES;
    }
    else
        
    {
        // NSLog(@"577符号");
        return NO;
    }
    //return results.count > 0;
}
-(BOOL)isPassword:(NSString *)getString
{
    return [self compare:getString];
}
- (BOOL)compared:(NSString *)IDString
{
    NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
    //符合數字條件的有幾個字元
    NSInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:IDString
                                
                                                                      options:NSMatchingReportProgress
                                                                        range:NSMakeRange(0, IDString.length)];
    //英文字條件
    
    NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];
    //符合英文字條件的有幾個字元
    
    NSInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:IDString options:NSMatchingReportProgress range:NSMakeRange(0, IDString.length)];
    if (tNumMatchCount == IDString.length)
        
    {
        //全部符合數字，表示沒有英文
        // NSLog(@"123数字");
        return YES;
    }
    
    else if (tLetterMatchCount == IDString.length)
        
    {
        //NSLog(@"345字母");
        //全不符合英文，表示沒有數字
        return NO;
    }
    
    else if (tNumMatchCount + tLetterMatchCount == IDString.length)
        
    {
        //NSLog(@"456数字字母");
        //符合英文和符合數字條件的相加等於密碼長度
        return YES;
    }
    else
        
    {
        // NSLog(@"577符号");
        return NO;
    }
    //return results.count > 0;
}
-(BOOL)isIDstring:(NSString *)IDString
{
    return [self compared:IDString];
}

- (BOOL)isQQ
{
    // 1.不能以0开头
    // 2.全部是数字
    // 3.5-11位
    return [self match:@"^[1-9]\\d{4,10}$"];
}
-(BOOL)isQQemail
{
    return [self match:@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"];
}
- (BOOL)isPhoneNumber
{
    // 1.全部是数字
    // 2.11位
    // 3.以13\15\18\17开头
    return [self match:@"^1[34578]\\d{9}$"];
    // JavaScript的正则表达式:\^1[34578]\\d{9}$\
    
}

- (BOOL)isIPAddress
{
    // 1-3个数字: 0-255
    // 1-3个数字.1-3个数字.1-3个数字.1-3个数字
    return [self match:@"^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"];
}

@end
